package com.sitc.street_lab_2013;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class HomeListViewActivity extends Activity implements OnItemClickListener {

	public static final String[] titles = new String[] { "Scan and Play",
		"Find a Place of Interest", "Find Techlab", "Help", "About" };

	public static final Integer[] images = { R.drawable.qrcode, R.drawable.find, R.drawable.marker, R.drawable.help, R.drawable.about };

	private static final int TECH_LAB_ID = 1;
	ListView listView;
	List<RowItem> rowItems;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.menu);

		rowItems = new ArrayList<RowItem>();
		for (int i = 0; i < titles.length; i++) {
			RowItem item = new RowItem(images[i], titles[i]);
			rowItems.add(item);
		}

		listView = (ListView) findViewById(R.id.list);
		CustomListViewAdapter adapter = new CustomListViewAdapter(this,
				R.layout.list_item, rowItems);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);    
		
		//ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
	    //HomeImageScrollAdapter scrollAdapter = new HomeImageScrollAdapter(this);
	    //viewPager.setAdapter(scrollAdapter);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	private boolean isNetworkConnected() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni == null) {
			// There are no active networks.
			Toast.makeText(getApplicationContext(), R.string.network_required, Toast.LENGTH_LONG).show();
			return false;
		} else
			return true;
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position == 0) {
			Intent quizIntent = new Intent(HomeListViewActivity.this, QuizActivity.class);
			HomeListViewActivity.this.startActivity(quizIntent);
			/*
			Intent qrIntent = new Intent(HomeListViewActivity.this,CameraPreview.class);
			HomeListViewActivity.this.startActivity(qrIntent); */

		} else if (position == 1) {

			if(isNetworkConnected())
			{
				Intent mapIntent = new Intent(HomeListViewActivity.this,LocationsListViewActivity.class);
				HomeListViewActivity.this.startActivity(mapIntent);
			}

		} else if (position == 2) {
			
			//Intent quizIntent = new Intent(HomeListViewActivity.this, QuizActivity.class);
			//HomeListViewActivity.this.startActivity(quizIntent);
			
			Intent locationIntent = new Intent(HomeListViewActivity.this, MapViewActivity.class);
			
			// Pass the Techlab location
			ArrayList<SitcLoc> locs = LocConstants.populate(); 
			locationIntent.putExtra("location", locs.get(TECH_LAB_ID));
			HomeListViewActivity.this.startActivity(locationIntent);

		} else if (position == 3) {

			Intent mainIntent = new Intent(HomeListViewActivity.this,HelpActivity.class);
			HomeListViewActivity.this.startActivity(mainIntent);

		} else if (position == 4) {
			
			Intent aboutIntent = new Intent(HomeListViewActivity.this,AboutUsActivity.class);
			HomeListViewActivity.this.startActivity(aboutIntent);
			
		}

	}

}
